package com.sctvcloud.linshui.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.utils.ListUtils;
import com.sctvcloud.linshui.R;
import com.sctvcloud.linshui.base.BaseFragment;
import com.sctvcloud.linshui.beans.ShortVideoTypeBean;
import com.sctvcloud.linshui.beans.ShortVideoTypeVo;
import com.sctvcloud.linshui.http.AbsNetCallBack;
import com.sctvcloud.linshui.http.NetUtils;
import com.sctvcloud.linshui.ui.adapter.ShortVideoPagerAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 分类短视频主页
 */
public class ShortVideoHomeFragment extends BaseFragment implements OnTabSelectListener {
    @BindView(R.id.short_video_tablay)
    protected SlidingTabLayout short_video_tablay;
    @BindView(R.id.short_video_content_vp)
    protected ViewPager short_video_content_vp;
    @BindView(R.id.short_video_top_lay_tab)
    protected ViewGroup tabLay;
    @BindView(R.id.short_video_content_ll)
    protected LinearLayout content_ll;
    @BindView(R.id.short_video_no_data_ct)
    protected CustomFontTextView no_data;
    private final String columnUrl = "sctv/shortvideonode/index/shortvideonode.json";
    private ShortVideoPagerAdapter pagerAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_short_video_home, container, false);
        ButterKnife.bind(this, view);
//        fragmentManager = this.getChildFragmentManager();
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getColumn();
    }



    /**
     * 获取首页栏目数据
     */
    private void getColumn() {
        NetUtils.getNetAdapter().getShortVideoChannel(getOwnerName(), columnUrl, new AbsNetCallBack<ShortVideoTypeVo>(ShortVideoTypeVo.class) {
            @Override
            public void onSuc(ShortVideoTypeVo vo) {
                if (null!=vo&& ListUtils.isListValued(vo.getChannel())) {
                    setData(vo.getChannel());
                } else {
                    no_data.setVisibility(View.VISIBLE);
                    content_ll.setVisibility(View.GONE);
                }
            }

            @Override
            public void onError(Throwable ex, String errorMsg) {
                super.onError(ex, errorMsg);
                no_data.setVisibility(View.VISIBLE);
                content_ll.setVisibility(View.GONE);
            }

            @Override
            public void onEnd() {
                super.onEnd();
                closeProgress();
            }
        });
    }

    /**
     * 加载 首页中的各个栏目页面
     *
     * @param fChannels
     */
    private void setData(List<ShortVideoTypeBean> fChannels) {
        if (ListUtils.isListValued(fChannels)) {
            content_ll.setVisibility(View.VISIBLE);
            no_data.setVisibility(View.GONE);
            boolean b = true;
            int s = -1;
            pagerAdapter = new ShortVideoPagerAdapter(getChildFragmentManager(), fChannels);
            short_video_content_vp.setAdapter(pagerAdapter);
            short_video_content_vp.setOffscreenPageLimit(1);
            short_video_tablay.setViewPager(short_video_content_vp);
            s = pagerAdapter.getCount();
            short_video_tablay.setOnTabSelectListener(this);
//            JLog.e("uuju  will to refresh column  b="+b+"   s="+s);
            refreshColumnTab();

//            short_video_tablay.setTextSelectColor(getResources().getColor(R.color.white));
//            short_video_tablay.setTextUnselectColor(getResources().getColor(R.color.color_66ffffff));
//            short_video_tablay.setIndicatorColor(getResources().getColor(R.color.white));
//            short_video_tablay.setUnderlineColor(getResources().getColor(R.color.color_FE6363));

        } else {
            no_data.setVisibility(View.VISIBLE);
            content_ll.setVisibility(View.GONE);
        }
    }

    private void refreshColumnTab() {
        int size = pagerAdapter.getCount();
        if (size == 0) {
            return;
        }
        short_video_tablay.setTabSpaceEqual(false);
        float d = getResources().getDimension(R.dimen.tab_home_padding_m);
        if (size <= 6) {
            d = getResources().getDimension(R.dimen.tab_home_padding_l);
        } else if (size == 7) {
            d = getResources().getDimension(R.dimen.tab_home_padding);
        }
        short_video_tablay.setTabPaddingPx(d);
        short_video_tablay.notifyDataSetChanged();
    }

    @Override
    public void onTabSelect(int position) {

    }

    @Override
    public void onTabReselect(int position) {

    }

    @Override
    public void onTabScrollTo(int position) {

    }
}
